package com.czbank.financing.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czbank.financing.Entity.NewProduct;
import com.czbank.financing.Entity.Product;
import com.czbank.financing.Entity.test;
import com.czbank.financing.service.ProductService;
import com.czbank.financing.test.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: thinkpad
 * @Date: 2019/7/12 18:25
 * @Description:
 */
@CrossOrigin
@RestController
@RequestMapping("/Product")
public class ProductController{

    @Autowired
    private ProductService productService;


    @RequestMapping(value = "/ProductInfo",method = RequestMethod.POST)
    public Object ProductInfoOne(@RequestBody JSONObject  jsonObject){
        System.out.println("json = " + jsonObject);
        String Object=jsonObject.getString("raccount");
        System.out.println("Object = " + Object);
        return productService.SelectProductInfo(Object);
    }
    @RequestMapping(value = "/ProductInfoPage",method = RequestMethod.POST)
    public Object findAllProductInfoPage(@RequestBody JSONObject jsonObject){
        int currPage = Integer.parseInt(jsonObject.getString("currentPage"));
        System.out.println("currPage = " + currPage);
        System.out.println("product = " + JSON.toJSONString(productService.selectAllProduct(currPage,10)));
       return JSON.toJSONString(productService.selectAllProduct(currPage,10));

    }

    @RequestMapping("/UpdateProduct")
    public Object UpdateProduct(Product product){
        if(productService.UpdateProductInfo(product)==0){
            return 0;
        }
        else {
             return 1;
        }
    }

    @RequestMapping("/DeleteProduct")
    public Object DeleteProduct(@RequestBody JSONObject jsonObject){
        String productName=jsonObject.getString("pid");
        System.out.println("productName = " + productName);

        if(productService.DeleteProductInfo(productName)==0){
            return "fail";
        }
        else {
            return "success";
        }
    }

    @RequestMapping("/InsertProduct")
    public Object InsertProductInfo(@RequestBody JSONObject jsonObject){

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        System.out.println("jsonObject = " + jsonObject);
        Product product = new Product();
        product.setPid(Integer.parseInt(jsonObject.getString("pid")));
        product.setProductName(jsonObject.getString("productName"));
        product.setProductLimit(Integer.parseInt(jsonObject.getString("productLimit")));
        product.setProductPrice(Integer.parseInt(jsonObject.getString("productPrice")));
        product.setProductProfits(Float.parseFloat(jsonObject.getString("productProfits")));
        product.setProductType(Integer.parseInt(jsonObject.getString("productType")));
        product.setCreationTime(dateString);

        System.out.println("product = " + product);
        if (productService.InsertProductInfo(product)==0){
            return 0;
        }
        else {
            return 1;
        }


    }

//    @RequestMapping("/cheapSaleGoodsList")
//    public Object dasad(@RequestBody JSONObject jsonObject){
//        System.out.println("jsonObject = " + jsonObject);
//        test test1 = new test();
//        test1.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563313461209&di=e98419bad66c4dd8ac0265c9eb5e3cf3&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D514814849%2C3939905761%26fm%3D214%26gp%3D0.jpg");
//        ArrayList<test> ls = new ArrayList<>();
//        ls.add(test1);
//        return JSON.toJSONString(ls);
//    }

    @RequestMapping("/allProduct")
    public Object TotalProduct(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        return JSON.toJSONString(productService.TotalProduct());
    }

    @RequestMapping("/cheapSaleGoodsList")
    public Object cheapSaleGoodsList(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        NewProduct np=new NewProduct();
        np.setGoodsName("理财基金");
        np.setGoodsSalePrice(90.1);
        np.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563313461209&di=e98419bad66c4dd8ac0265c9eb5e3cf3&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D514814849%2C3939905761%26fm%3D214%26gp%3D0.jpg");

        NewProduct np1=new NewProduct();
        np1.setGoodsName("股票");
        np1.setGoodsSalePrice(56.9);
        np1.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563313461209&di=e98419bad66c4dd8ac0265c9eb5e3cf3&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D514814849%2C3939905761%26fm%3D214%26gp%3D0.jpg");

        List<NewProduct> list=new ArrayList<NewProduct>();
        list.add(np);
        list.add(np1);
        return JSON.toJSONString(list);
    }



    @RequestMapping("/hotSaleGoodsDataList")
    public Object hotSaleGoodsDataList(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        NewProduct np=new NewProduct();
        np.setGoodsName("理财基金");
        np.setGoodsSalePrice(90.1);
        np.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563340782263&di=e98ba093658869e94af2c97722efadcb&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171003%2F1981c60b2a9e44dca95c22be10d2c215.jpeg");

        NewProduct np1=new NewProduct();
        np1.setGoodsName("股票");
        np1.setGoodsSalePrice(56.9);
        np1.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563340806829&di=1bc0a44b47a1bc447dcbd084b6e27708&imgtype=0&src=http%3A%2F%2Fupload.ikanchai.com%2F2018%2F0227%2F1519739776829.jpg");

        List<NewProduct> list=new ArrayList<NewProduct>();
        list.add(np);
        list.add(np1);
        return JSON.toJSONString(list);
    }

    @RequestMapping("/newSaleGoodsDataList")
    public Object newSaleGoodsDataList(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        NewProduct np=new NewProduct();
        np.setGoodsName("理财基金");
        np.setGoodsSalePrice(90.1);
        np.setGoodsBigPic("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1437062119,1255420841&fm=26&gp=0.jpg");

        NewProduct np1=new NewProduct();
        np1.setGoodsName("股票");
        np1.setGoodsSalePrice(56.9);
        np1.setGoodsBigPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563341004955&di=873c883dca085487a89816f7bd12d584&imgtype=0&src=http%3A%2F%2Fstatic.csai.cn%2Fcsai_cms%2Fsite%2F2019-05%2F647%2F20190524172053866.jpg");
        List<NewProduct> list=new ArrayList<NewProduct>();
        list.add(np);
        list.add(np1);
        return JSON.toJSONString(list);
    }
}
